//
//  UILabel+LineSpace.m
//  DWCoach
//
//  Created by Mxionlly on 17/1/17.
//  Copyright © 2017年 周玉. All rights reserved.
//

#import "UILabel+LineSpace.h"

@implementation UILabel (LineSpace)

static char *lineSpaceKey;

+ (void)load {
    //交换设置文本的方法实现。
    Method oldMethod = class_getInstanceMethod([self class], @selector(setText:));
    Method newMethod = class_getInstanceMethod([self class], @selector(setHasLineSpaceText:));
    method_exchangeImplementations(oldMethod, newMethod);
}

- (void)setHasLineSpaceText:(NSString *)text {
    
    if (!text.length || self.lineSpace==0) {
        [self setHasLineSpaceText:text];
        return;
    }
    
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineSpacing = self.lineSpace;
    style.lineBreakMode = self.lineBreakMode;
    
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:text];
    [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
    self.attributedText = attrString;
}

- (void)setLineSpace:(CGFloat)lineSpace {
    
    objc_setAssociatedObject(self, &lineSpaceKey, @(lineSpace), OBJC_ASSOCIATION_ASSIGN);
    self.text = self.text;
}

- (CGFloat)lineSpace {
    return [objc_getAssociatedObject(self, &lineSpaceKey) floatValue];
}


/**
 为UILabel文字获取文字长度
 
 @param text 文本
 @param font 字体大小
 @param maxSize 大小
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (NSAttributedString *)textWithParagraphlineSpeace:(CGFloat)lineSpacing textFont:(UIFont *)font text:(NSString *)text {
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpacing;
    NSDictionary *attributes = @{ NSParagraphStyleAttributeName:paragraphStyle};
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
    NSDictionary * attriBute = @{NSFontAttributeName:font};
    [attriStr addAttributes:attriBute range:NSMakeRange(0, text.length)];
    return attriStr;
}

//--------- 复制功能
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copyText:));
}

- (void)attachTapHandler {
    self.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *g = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self addGestureRecognizer:g];
}

- (void)handleTap:(UIGestureRecognizer *)g {
    [self becomeFirstResponder];
    UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyText:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:item]];
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}

//  复制时执行的方法
- (void)copyText:(id)sender {
    //  通用的粘贴板
    UIPasteboard *pBoard = [UIPasteboard generalPasteboard];
    
    //  有些时候只想取UILabel的text中的一部分
    if (objc_getAssociatedObject(self, @"expectedText")) {
        pBoard.string = objc_getAssociatedObject(self, @"expectedText");
    } else {
        if (self.text) {
            pBoard.string = self.text;
        } else {
            pBoard.string = self.attributedText.string;
        }
    }
}

- (BOOL)canBecomeFirstResponder {
    return [objc_getAssociatedObject(self, @selector(isCopyable)) boolValue];
}

- (void)setIsCopyable:(BOOL)number {
    objc_setAssociatedObject(self, @selector(isCopyable), [NSNumber numberWithBool:number], OBJC_ASSOCIATION_ASSIGN);
    [self attachTapHandler];
}

- (BOOL)isCopyable {
    return [objc_getAssociatedObject(self, @selector(isCopyable)) boolValue];
}

- (UILabel *(^)(NSString *str,UIColor *color,CGFloat size))setupmoreColorFont{
    return ^(NSString *str,UIColor *color,CGFloat size) {
        NSDictionary *attributes = @{NSForegroundColorAttributeName:color,NSFontAttributeName:[UIFont systemFontOfSize:size]};
        if (self.attributedText.string.length<1) {
            self.attributedText = [[NSMutableAttributedString alloc]initWithString:str attributes:attributes];
        }else{
            NSMutableAttributedString *mutAttribute=[[NSMutableAttributedString alloc]initWithAttributedString:self.attributedText];
            [mutAttribute appendAttributedString:[[NSAttributedString alloc]initWithString:str attributes:attributes]];
            self.attributedText=mutAttribute;
            
            
        }
        
        
        return self;
    };
}
@end
